package org.thinker.point;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.thinker.dao.DAO;

public class PointServiceImpl implements PointService {
	
	private List<PointVO> list;
	
	private static PointServiceImpl instance = new PointServiceImpl();
	private List<PointVO> pointList = new ArrayList<PointVO>();
	private List<ResultVO> resultList = new ArrayList<ResultVO>();
	
	private  PointServiceImpl(){
		
	}
	
	public static PointServiceImpl getInstance(){
		return instance;
	}

	
	
	@Override
	public List<ResultVO> addPoint(PointVO vo) throws Exception {
		
		for(PointVO rel: pointList){
			resultList.add(rel.calcDistance(vo));
		}
		pointList.add(vo);
		try{
			DAO.inst.add(vo);			
		}catch (Exception e){
			e.printStackTrace();
		}		
		return resultList;
	}
	
	@Override
	public PointVO get(String name) throws Exception {
		return DAO.inst.get(name);
		
		
	}

	@Override
	public void create(PointVO vo) throws Exception {
		System.out.println("debug...create");
		DAO.inst.add(vo);
	}	
	
	
	@Override
	public void delete(String name) throws Exception {
		System.out.println("debug...delete");
		DAO.inst.remove(name);
	}		


	public List<ResultVO> find(PointVO vo) throws Exception {	
		List<PointVO> totalPoint = null;
		totalPoint = DAO.inst.totalList();
		List<ResultVO> sortList = new ArrayList<ResultVO>();
		for(PointVO pvo: totalPoint){
			resultList.add(pvo.calcDistance(vo));			
		}
		Collections.sort(resultList);
		for(int i = 0; i < 10; i++){
			sortList.add(resultList.get(i));
		}
		
		return sortList;
	}	

};
